<template>
  <div class="address">
    <van-nav-bar title="收货地址" left-arrow @click-left="$router.push('/user')" />
    <van-address-list
      v-model="chosenAddressId"
      :list="addressList && addressList.length > 0 ? addressList : addressList"
      :disabled-list="disabledList"
      disabled-text="以下地址超出配送范围"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
    />
  </div>
</template>

<script>
// import { Toast } from 'vant'
import { mapState } from 'vuex'
import loginConfirm from '@/mixins/loginConfirm'
export default {
  name: 'AddressList',
  mixins: [loginConfirm],
  data () {
    return {
      isLogin: false,
      chosenAddressId: '1',
      disabledList: [
        {
          id: '3',
          name: '王五',
          tel: '1320000000',
          address: '浙江省杭州市滨江区江南大道 15 号'
        }
      ]
    }
  },
  async created () {
    // 组件创建时检查登录状态
    const isLogin = await this.loginConfirm()
    this.isLogin = isLogin
    if (!isLogin) {
      await this.getAddressList()
    }
  },
  methods: {
    // 新增地址
    onAdd () {
      if (this.isLogin) {
        this.$router.push('/login')
        return
      }
      // 跳转地址编辑页
      this.$router.push('/addressedit')
    },
    onEdit (item, index) {
      // 跳转地址编辑页
      this.$router.push(`/addressedit/${index}`)
    },
    async getAddressList () {
      await this.$store.dispatch('address/getUserAddressList')
    }
  },
  computed: {
    ...mapState('address', ['addressList'])
  }
}
</script>

<style lang="less" scoped>

</style>
